<?php /*
	
*/ ?>
<?php include("system/config.inc.php");
$donotinclude = 1;
// IF admin is not logged in then it will redirect to the adminlogin page.
if(!isset($_SESSION['adminuserid']))
{
	header("location:index.php?msg=logfirst");
	die();
}

// INSERT into database.
if(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Submit")
	{
				
				$client=mysql_real_escape_string($_REQUEST['manuf']);
				$varthpath = $_FILES['imagefile']['name'];
				$desc=mysql_real_escape_string(str_replace("\r\n","<br />",$_REQUEST['desc']));
				$head=mysql_real_escape_string($_REQUEST['heading']);
				//$time= date("D: H : i : s : A");
				$insert="INSERT INTO `tblmanufactures` ( `varname` , `varimagepath` , `txtdesc`,`varheading`,`dtdateadded` , `dtlastmodified`) 
				VALUES ('$client', '$varthpath','$desc','$head',NOW(),NOW())";
				$sql=$obj_db->insert($insert);
				
				// get Id of inserted record
				$screen_id = mysql_insert_id($obj_db->CONN);
				
				// Image uploading
			if(isset($_REQUEST['uploading']) && trim($_REQUEST['uploading']) == "imageupload" && 					
			$_FILES['imagefile']['name']!="")
			{
				$targetpath = "../images/manuf/";
				$ext=$_FILES['imagefile']['name'];
				$ext=explode(".",$ext);
				if($ext[1]=="jpg" || $ext[1]=="gif" || $ext[1]=="jpeg" || $ext[1]=="png" || $ext[1]=="bmp" || $ext[1]=="wbmp" || $ext[1]=="JPEG" || $ext[1]=="JPG")
				{
				if($_FILES['imagefile']['size'] <= 2000000)
				{
				$filename=$targetpath.$ext[0].$screen_id.".".$ext[1];
				$fl_db=$ext[0].$screen_id.".jpg";
				
				if(file_exists($filename))
				{
				chmod($filename, 0777);
				unlink($filename);
				}
				if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$filename))
				{				
					$targetpath = "../images/manuf/";
					$file=createThumb($filename , $targetpath , $ext[0].$screen_id , $_SESSION['config_val'][0]['intcategoryimage']);
				$sql= "UPDATE tblmanufactures SET `varimagepath` = '".$fl_db."' WHERE intmanufactureid = '".$screen_id."' ";
				$res=$obj_db->sql_query($sql);
				}
				}
				}
			}
		header("location:manufacture.php?msg=add");
		die();
	}	

// UPDATE the record
$p_manuf = "";
$p_varthpath = "";
$p_desc="";
$p_head="";
$action=1;
if((isset($_REQUEST['a']) && trim($_REQUEST['a'])==2) && (!(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Submit")))
{
 	if(isset($_REQUEST['intid']) && trim($_REQUEST['intid'])!="")
 	{
		$screen_id =  $_REQUEST['intid'];
		$sql = "select * from tblmanufactures where intmanufactureid ='$screen_id'";
		$result = $obj_db->select($sql);
		if(count($result)<=0)
		{
			echo " No Record Found!<br>";
			die();
		}
		if($result)
		{
			foreach($result as $row)
			{
				$p_manuf = $row['varname'];	   
		   		$p_varthpath = $row['varimagepath'];
				$p_desc = $row['txtdesc'];	
				$p_head	=	$row['varheading'];		
				$action=2;
			}
		}
		if(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Update")
		{
				$client=mysql_real_escape_string($_REQUEST['manuf']);
				$varthpath = $_FILES['imagefile']['name'];
				$desc=mysql_real_escape_string(str_replace("\r\n","<br />",$_REQUEST['desc']));
				$head=mysql_real_escape_string($_REQUEST['heading']);
				$sql_upd ="UPDATE tblmanufactures SET  
						varname = '$client',
						txtdesc='$desc',
						dtdateadded=dtdateadded,
						varheading='$head',
						dtlastmodified=NOW() 
						 where intmanufactureid ='$screen_id'";
				$result = $obj_db->edit($sql_upd);
				$action=1;
			
			// update images
			if(isset($_REQUEST['uploading']) && trim($_REQUEST['uploading']) == "imageupload" && $_FILES['imagefile']['name']!="")
			{
				$targetpath = "../images/manuf/";
				$ext=$_FILES['imagefile']['name'];
				$ext=explode(".",$ext);
				if($ext[1]=="jpg" || $ext[1]=="gif" || $ext[1]=="jpeg" || $ext[1]=="png" || $ext[1]=="bmp" || $ext[1]=="wbmp" || $ext[1]=="JPEG" || $ext[1]=="JPG")
				{
					if($_FILES['imagefile']['size'] <= 2000000)
					{
						$filename=$targetpath.$ext[0].$screen_id.".".$ext[1];
						$fl_db=$ext[0].$screen_id.".jpg";
						
						if(file_exists($filename))
						{
							chmod($filename, 0777);
							unlink($filename);
						}
						if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$filename))
						{	
							$targetpath = "../images/manuf/";
							$file=createThumb($filename , $targetpath , $ext[0].$screen_id , $_SESSION['config_val'][0]['intcategoryimage']);
							$sql= "UPDATE tblmanufactures SET `varimagepath` = '".$fl_db."' WHERE intmanufactureid = '".$screen_id."' ";
							$res=$obj_db->sql_query($sql);
							chmod($filename, 0777);
							unlink($filename);
						}
					}				
				}								
			}
			header("location:manufacture.php?msg=edit");
			die();
		}
	}
}

//DELETE record from database
if(isset($_REQUEST['a']) && trim($_REQUEST['a'])==3)
{
	if(isset($_REQUEST['intid']) && trim($_REQUEST['intid']!=""))
	{	
		$id =  $_REQUEST['intid'];
		$sql= "DELETE FROM `tblmanufactures` WHERE `intmanufactureid` ='$id'";
		$query1 = mysql_query($sql);
		header("location:manufacture.php?msg=del");
		die();
	}
}

?>
<script language="javascript">
function check()
{
	var cond=true;
	if(document.manufa.manuf.value==0)
	{
		alert("Please Enter Name.");
		if(cond==true)
		{
			document.manufa.manuf.focus();
		}
		cond=false;
		return false;
	}	
	
}
</script>
<?php include("inc/header.php");?>
<body>


<table width="80%" border="0" align="center" cellpadding="0" cellspacing="1" class="middlebackground">
  <tr>
    <td colspan="3" valign="top" align="center"><?php include(INC."top.inc.php");?></td>
  </tr>
  <tr>
    <td width="2%">&nbsp;</td>
    <td width="95%"><?php   
   if(isset($_REQUEST['script']))
	{
	if(trim($_REQUEST['script'])=='addmanuf' || trim($_REQUEST['script'])=='editmanuf')
	{
	?>
		<form action="" method="post" enctype="multipart/form-data" name="manufa">
		
      <table width="60%" border="0" align="center" cellpadding="2" cellspacing="2" class="border">
        <tr class="bg1">
          <td colspan="5" align="center"><strong>
            <?php if($_REQUEST['script']=='addmanuf'){echo "Add New Brand...";}
	if($_REQUEST['script']=='editmanuf'){echo "Edit Brand...";}?>
          </strong></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td align="left">&nbsp;</td>
          <td>&nbsp;</td>
          <td align="right">(* All fields are Required)</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td align="right" class="sltimg">*Main Heading </td>
          <td>&nbsp;</td>
          <td align="left"><input name="heading" type="text" id="heading" value="<?php echo stripslashes($p_head);?>" /></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td width="2%">&nbsp;</td>
          <td width="25%" align="right" class="sltimg">*Name</td>
          <td width="4%">:</td>
          <td width="63%" align="left"><label>
            <input name="manuf" type="text" id="manuf" value="<?php echo stripslashes($p_manuf);?>" />
          </label></td>
          <td width="6%">&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td align="right" class="sltimg">*Image</td>
          <td>:</td>
          <td align="left"><input name="imagefile" type="file" id="imagefile" /></td>
          <td>&nbsp;</td>
        </tr>

        <tr>
          <td align="left">&nbsp;</td>
          <td align="right">*Description</td>
          <td align="left">&nbsp;</td>
          <td align="left"><textarea name="desc" cols="30" rows="4" id="desc"><?php echo str_replace("<br />","\r\n",stripslashes($p_desc));?></textarea></td>
          <td align="left">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="5" align="left">&nbsp;
              <input type="hidden" name="uploading" value="imageupload" /></td>
        </tr>
        <tr>
          <td colspan="5" align="center"><input name="Submit" type="submit" class="btn" id="Submit" value="<?php echo ($action==2) ? "Update":"Submit"; ?>"  onclick="return check();"/></td>
        </tr>
        <tr>
          <td colspan="5" align="center">&nbsp;</td>
        </tr>
      </table>	</form>

    <?php
	}
}
else
{
?>
<br /><br />
    <table width="80%" border="0" align="center" cellpadding="2" cellspacing="2" class="border">
      <tr class="bg1">
        <td colspan="5" align="left"><strong>Brands</strong>...</td>
        <td align="center"><a href="manufacture.php?script=addmanuf" class="new">[NEW]</a></td>
      </tr>
      <tr>
         <?php 
	   $rows="";
	 if(isset($_REQUEST['page']) && ($_REQUEST['page'])!="")
	 {
	 	$rows=$_REQUEST['page'];
	 
	  }	
	  ?><form name="form1" action="" method="post">
              <td colspan="6" align="left">
			  
			  Row per Page:
                <select name="page" onChange="document.form1.submit();">
                  <option value="0"<?php if($rows=='0'){echo "selected";}else{echo "";}?>>All</option>
                  <option value="5"<?php if($rows=='5'){echo "selected";}else{echo "";}?>>5 Per Page</option>
                  <option value="10"<?php if($rows=='10'){echo "selected";}else{echo "";}?>>10 Per Page</option>
                  <option value="15"<?php if($rows=='15'){echo "selected";}else{echo "";}?>>15 Per Page</option>
                </select>
				
			</td>
				</form>
        </tr>
      <tr class="tblbg3">
        <td align="center"><strong>
		<?php 
			if(isset($_REQUEST['sort']) && ($_REQUEST['sort'])=='alphaasc')
			{
		?>
		<a  class="ab1" href="manufacture.php?sort=alphadesc">Name</a>
		<?php 
			}
			else if(isset($_REQUEST['sort']) && ($_REQUEST['sort'])=='alphadesc')
			{
		?><a class="ab1" href="manufacture.php?sort=alphaasc">Name</a>
		<?php }
			else
			{
			 ?>
			 <a class="ab1" href="manufacture.php?sort=alphadesc">Name</a>
			 <?php
		 }?>
		</strong></td>
		
        
        <td align="center"><strong>Description</strong></td>
        <td align="center"><strong>Image</strong></td>
        <td align="center"><strong>Date</strong></td>
        
        <td align="center"><strong>Edit</strong></td>
        <td align="center"><strong>Delete</strong></td>
      </tr>
      <?php	  
	  	/**************************************
				PAGING CODE START
		**************************************/
			if(isset($_REQUEST['page']) && ($_REQUEST['page'])!=0)
			{
				$rows=$_REQUEST['page'];
				$rowperpage=$rows;
			}else{$rowperpage=$rows=10000;}
			$tablename="tblmanufactures";
			$per_page_keywords= "";
			$per_page_sorts="";
			include("system/paging.inc.php");

		/**************************************
				PAGING CODE ENDING
		**************************************/
		if(isset($_REQUEST['sort']) && ($_REQUEST['sort'])=='alphaasc')
		{
			$sql="select * from tblmanufactures ORDER BY `varname` ASC Limit ".($page_no*$row_per_page).",".$row_per_page;
			//$result=$obj_db->select($sql);
			//print_r($result);
			//echo $sql;
		}
		else if(isset($_REQUEST['sort']) && ($_REQUEST['sort'])=='alphadesc')
		{
   		$sql="select * from tblmanufactures ORDER BY `varname` DESC Limit ".($page_no*$row_per_page).",".$row_per_page;
		}
		else
		{
		$sql="select * from tblmanufactures Limit ".($page_no*$row_per_page).",".$row_per_page;
		}
		$result=$obj_db->select($sql);
  		if($result)
		{
   		for($i=0;$i<count($result);$i++)
   		{
            $id=$result[$i]['intmanufactureid'];
			
			$imagepath = "../images/manuf/";
			$limit=$result[$i]['varimagepath'];
			$imagename = $imagepath.$limit;
			?>
      <tr class="<?php echo ($i%2==0)?"Hrnormal":"Hralter"; ?>" onMouseOver="this.className='Hrhover';"  onmouseout="this.className='<?php echo ($i%2==0)?"Hrnormal":"Hralter"; ?>';">
        <td align="center"><?php 
		
		echo stripslashes($result[$i]['varname']);
		
		
		?></td>
        <td align="center"><?php 
		$desc= $result[$i]['txtdesc'];
		if(strlen($desc)>10)
		{
			$com=substr($desc,0,10);
			echo $com."....";
		}
		else{echo stripslashes($desc);}
		
		?></td>
        
        <td align="center">
		
	
		<img title="'<?php echo $result[$i]['varname'];?>'" src="<?php echo $imagename;?>" width="100" height="100"/>		</td>
        <td align="center"><?php echo printdate($result[$i]['dtlastmodified']);?></td>
        
        <td align="center"><a class="link" Title="Click here to Edit" href="manufacture.php?script=editmanuf&amp;a=2&amp;intid=<?php echo $result[$i]['intmanufactureid']; ?>"><img src="images/edit.bmp" border="0"/></a></td>
        <td align="center"><a Title="Click here to Delete" href="manufacture.php?a=3&amp;intid=<?php echo $result[$i]['intmanufactureid']; ?>" onClick="return confirm('Are you sure to delete this record?');"><img src="images/delete.bmp" border="0"/></a></td>
      </tr>
      <?php }?>
      <tr>
        <td colspan="6" align="center"><?php
pagindet_atbotttom_page($div_page_no,$page_no,$req_querystr,$total_db_rec,$row_per_page);
?></td>
      </tr>
    </table>
    <?php }}?>
	</td>
    <td width="3%">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3"><?php include("inc/bottom.php");?></td>
  </tr>
</table>


